/**
 * This class provides an interface for a physical communication device
 * module.
 * 
 * @author Roman Krashanitsa
 * @version 0.1 June 2007
 */

package pprz.data_components;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.swing.event.EventListenerList;

import pprz.data_components.events.DataEvent;
import pprz.data_components.events.DataEventListener;

public class Device {

	public boolean ok;
	
	protected String portName;
	
	protected EventListenerList listenerList;
	protected List<PropertyChangeListener> propertyChangeListeners;
	
	public Device()	
	{
		listenerList = new EventListenerList();
		propertyChangeListeners = new LinkedList<PropertyChangeListener>();
		ok=false;
		portName="";
	}	
	
	public void init(){};
	
    // This methods allows classes to register for MyEvents
    public void addDataEventListener(DataEventListener listener) {
        listenerList.add(DataEventListener.class, listener);
    }

    // This methods allows classes to unregister for MyEvents
    public void removeDataEventListener(DataEventListener listener) {
        listenerList.remove(DataEventListener.class, listener);
    }
    // This protected class is used to fire MyEvents
    void fireDataEvent(DataEvent evt) {
        Object[] listeners = listenerList.getListenerList();
        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i]==DataEventListener.class) {
                ((DataEventListener)listeners[i+1]).dataEvent(evt);
            }
        }
    }
    
    public String getPortName(){return portName;}

    public void setPortName(String name){firePropertyChangeEvent("port",portName,name); this.portName=name;}
    
    public boolean getState(){return ok;}

    public void setState(boolean isOk){firePropertyChangeEvent("state",ok,isOk); this.ok=isOk;}
    
    
    public void addPropertyChangeListener(PropertyChangeListener propertyChangeListener) {
        if(propertyChangeListener == null){
            throw new IllegalArgumentException("ERROR:MeposeProject.addPropertyChangeListener(...):Parameter 'propertyChangeListener' is null.");
        }
        this.propertyChangeListeners.add(propertyChangeListener);
    }
    
    public void removePropertyChangeListener(PropertyChangeListener propertyChangeListener) {
        if(propertyChangeListener == null){
            throw new IllegalArgumentException("ERROR:MeposeProject.removePropertyChangeListener(...):Parameter 'propertyChangeListener' is null.");
        }
        this.propertyChangeListeners.remove(propertyChangeListener);
    }
    
    public void firePropertyChangeEvent(String property,Object oldValue, Object newValue) {
        if(property == null){
            throw new IllegalArgumentException("ERROR:MeposeProject.firePropertyChangeEvent(...):Parameter 'property' is null.");
        }
        PropertyChangeEvent event = new PropertyChangeEvent(this,property,oldValue,newValue);
        for (Iterator iterator = this.propertyChangeListeners.iterator(); iterator.hasNext(); ) {
            PropertyChangeListener propertyChangeListener = (PropertyChangeListener) iterator.next();
            propertyChangeListener.propertyChange(event);
        }
    }
    
    
}
